package com.rrd.itext.xmlBean.layoutBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Element;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.rrd.itext.utils.PDFBuilder;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementParser;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("ListItem")
public class XmlListItem implements PdfElementParser<Paragraph>{
	@XStreamAsAttribute
	private int printOrder;
	
	@XStreamAlias("Chunks")
	private List<XmlChunk> chunks;
	
    @XStreamAlias("HTMLBlock")
    private XmlHtmlBlock htmlBlock;
	
	private Comparator<XmlChunk> chunkComparator;

	public Comparator<XmlChunk> getChunkComparator() {
		if(chunkComparator == null){
			this.chunkComparator = new Comparator<XmlChunk>(){
				@Override
				public int compare(XmlChunk o1, XmlChunk o2) {
					return o1.getPrintOrder()-o2.getPrintOrder();
				}};
		}
		return chunkComparator;
	}

	public void setChunkComparator(Comparator<XmlChunk> chunkComparator) {
		this.chunkComparator = chunkComparator;
	}

	public int getPrintOrder() {
		return printOrder;
	}

	public void setPrintOrder(int printOrder) {
		this.printOrder = printOrder;
	}

	public List<XmlChunk> getChunks() {
		if(this.chunks!=null){
			Collections.sort(chunks,getChunkComparator());
		}else{
			chunks = new ArrayList<XmlChunk>();
		}
		
		return chunks;
	}

	public void setChunks(List<XmlChunk> chunks) {
		this.chunks = chunks;
	}

    public XmlHtmlBlock getHtmlBlock() {
        return htmlBlock;
    }

    public void setHtmlBlock(XmlHtmlBlock htmlBlock) {
        this.htmlBlock = htmlBlock;
    }

    @Override
    public Paragraph parse(PDFBuilder builder) throws Exception {
        Paragraph para = new Paragraph();
        ListItem item = new ListItem();
        List<XmlChunk> xmlChunks = this.getChunks();
        XmlHtmlBlock htmlBlock = this.getHtmlBlock();
        if(htmlBlock != null){
            String name = htmlBlock.getName();
            if(builder.getResourceMap().containsKey(name)){
                List<Element> elments = (List<Element>)htmlBlock.parse(builder);
                para.addAll(elments);
                item.addAll(elments);
            }
        }else{
            for (XmlChunk xmlChunk : xmlChunks) {
                if (xmlChunk.getNewLine()) {
                    para.add(Chunk.NEWLINE);
                    item.add(Chunk.NEWLINE);
                }
                Chunk ck = xmlChunk.parse(builder);
                if(ck != null){
                    para.add(ck);
                    item.add(ck);
                }
            }
        }
        if(para.size() == 0){
            return null;
        }
        return item;
    }
	
}
